home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 646 / jukebx14 / src / irqtimer.c next >
C/C++ Source or Header  |  1992-04-28  |  3KB  |  166 lines

  1. #include <tos.h>
  2. #include <stdio.h>
  3. #include <aes.h>
  4.  
  5. #include "irqtimer.h"
  6.  
  7.  
  8. static long get_ticks_120mS(void)
  9. {volatile long *_hz_200 = (long *)0x04BA;
  10.  long time = 120/5;    /* 120 mS in ticks */
  11.  long n_ticks = 0;
  12.  long test;
  13.      
  14.      /* wait until the timer ticks! 
  15.       */
  16.      test = *_hz_200;
  17.      while(test == *_hz_200);
  18.      
  19.      /* count the number of loops in 120 mS 
  20.       */
  21.     time += *_hz_200;    /* what time is it i 120 mS ? */    
  22.     while (time > *_hz_200)
  23.     {
  24.         n_ticks++;
  25.     }
  26.     
  27.     return n_ticks;
  28. }
  29.  
  30.  
  31. static long sync_ticks(void)
  32. {long free_run;
  33.  int  i;
  34.  
  35.     Supexec(get_ticks_120mS);
  36.  
  37.     free_run = 0;
  38.     for (i = 1; i <= 16; i++)
  39.         free_run += Supexec(get_ticks_120mS);
  40.     
  41.     /* round and take mean
  42.      */
  43.     return (free_run+8) / 16;
  44. }
  45.  
  46.  
  47. static long get_mean_ticks(int  resync)
  48. {static int index = 0;
  49.  static long run[16];
  50.  int    i;
  51.  long   mean;
  52.  
  53.      if (resync)
  54.      {
  55.         mean = sync_ticks();
  56.     
  57.          /* copy sync-ticks into the mean array
  58.          */
  59.         for (i = 0; i < 16; run[i++] = mean);
  60.         
  61.         return mean;
  62.     }
  63.     
  64.     index++;
  65.     index &= 0xF;
  66.     
  67.     run[index] = Supexec(get_ticks_120mS);
  68.     
  69.     mean = 0;
  70.     for (i = 0; i < 16; i++)
  71.         mean += run[i];
  72.     
  73.     /* round and take mean
  74.      */
  75.     return (mean+8) / 16;
  76. }
  77.     
  78.     
  79. main()
  80. {long free_run, run, total;
  81.  double ratio;
  82.  OBJECT *menu;
  83.  int    msg[8], mx, my, mb, ks, kr, br, which;
  84.  int    more, Index, i;
  85.  
  86.      appl_init();
  87.      rsrc_load("irqtimer.rsc");
  88.      
  89.     rsrc_gaddr(0,MENU,&menu);
  90.      menu_bar(menu, 1);
  91.  
  92.     graf_mouse(ARROW, 0);
  93.     
  94.     free_run = get_mean_ticks(1);
  95.  
  96.  
  97.      more = 1;
  98.      Index = 0;
  99.      
  100.      do
  101.      {
  102.         which = evnt_multi(MU_MESAG | MU_TIMER,
  103.             0x102, 0x03, 0x00,
  104.             0, 0,0,0,0,     0, 0,0,0,0,
  105.             msg,
  106.             500, 0,
  107.             &mx, &my, &mb, &ks, &kr, &br);
  108.             
  109.         if (which & MU_MESAG && msg[0] == MN_SELECTED)
  110.          {
  111.              switch (msg[4])
  112.              {
  113.              case ABOUT:
  114.                  form_alert(1,
  115.                     "[0][To Index the a background|"
  116.                         "program - disable it and|"
  117.                         "select Sync. Then start it,|"
  118.                         "and Messure.][ OK ]");   
  119.                  break;
  120.              case SYNC:
  121.                 free_run = get_mean_ticks(1);
  122.                  Index = 0;
  123.                  break;
  124.              case INDEX:
  125.                  Index ^= 1;
  126.                 total = 0;
  127.                  i = 0;
  128.                 printf("\033H\n\n\n\n");
  129.                  break;
  130.              case QUIT:
  131.                  more = 0;
  132.              }
  133.              menu_tnormal(menu,msg[3],1);
  134.          }
  135.          
  136.          if (which & MU_TIMER && Index)
  137.          {
  138.             ++i;
  139.             run = get_mean_ticks(0);
  140.             total += run;
  141.         
  142.             ratio = 100.0*(free_run-run) / (double)free_run;
  143.             wind_update(BEG_UPDATE);
  144.             
  145.             printf("%7.1lf", ratio);
  146.                 
  147.             if (i&7)
  148.                 printf(",");
  149.             else
  150.             {
  151.                 ratio = 100.0/8.0*(8*free_run-total) / (double)free_run;        
  152.                 printf(" = <%7.1lf >\033H\n\n\n\n",ratio);
  153.                 total = 0;
  154.             }
  155.             
  156.             wind_update(END_UPDATE);
  157.             
  158.         }             
  159.     } while (more);    
  160.     
  161.      menu_bar(menu, 0);
  162.      rsrc_free();
  163.     appl_exit();
  164.     
  165.     return 0;